package com.contract.spirit.ui.invite

import android.content.Context
import android.content.Intent
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.refresh.IRefreshCallBack
import com.contract.commonlib.refresh.RefreshManager
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.databinding.ActivityMyInviteUserBinding

/**
 * @author: witness
 * created: 2021/9/28
 * desc: 我的团队 /我的邀请用户列表
 */
class MyInviteUserActivity : BaseActivity<ActivityMyInviteUserBinding,InviteViewModel>() {
    var pageNum : Int = 1
    private val inviteUserAdapter by lazy {
        MyInviteUserAdapter()
    }
    override fun setContentView(): Int {
        return R.layout.activity_my_invite_user
    }

    override fun initData() {
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvUser,mContext)
        inviteUserAdapter.setFooterView(footView)
        mBinding.rvUser.adapter = inviteUserAdapter
        getInviteUserInfo()
    }

    override fun onReload() {
        mViewModel.inviteRecordRes.value = null
        getInviteUserInfo()
    }

    override fun initRefresh() {
        RefreshManager.getInstance().initRefresh(mContext,footView,true,mBinding.refreshLayout,object : IRefreshCallBack{
            override fun onRefresh() {
                pageNum = 1
                getInviteUserInfo()
            }
            override fun onLoadMore() {
                pageNum++
                getInviteUserInfo()
            }
        })
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.inviteRecordRes.observe(this,{ it ->
            parseState(it,{
                if (pageNum==1){
                    inviteUserAdapter.setList(it.inviteList)
                }else{
                    inviteUserAdapter.addData(it.inviteList)
                }
                setDataLoadFinish(pageNum,it.inviteList.size,mBinding.refreshLayout)
                val inviteCount = it.inviteCount;
                mBinding.titleBar.setTitleName("我的团队($inviteCount)")
            },{
                endLoad(pageNum,mBinding.refreshLayout)
            })
        })
    }

    private fun getInviteUserInfo(){
        mViewModel.getInviteUserInfo(pageNum)
    }

    companion object{
        fun launch(mContext: Context){
            mContext.startActivity(
                Intent(
                    mContext,
                    MyInviteUserActivity::class.java
                )
            )
        }
    }
}